﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using GameStudio.Common.BootStrappers;
using GameStudio.IDE.Common.Events;
using Microsoft.Practices.ServiceLocation;
using Utils.EventAggregator;
using GameStudio.Common;

namespace GameStudio.IDE.Common.Bootstrappers
{
    class ListenOnProjectChangesTask : IBootstrapper
    {
        public void Run()
        {
            var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            eventAggregator.GetEvent<ChangeProjectEvent>().Subscribe(OnProjectChanges);
        }

        [PermissionSet(SecurityAction.Demand,Name="FullTrust")]

        private void OnProjectChanges(Project project)
        {
            FileSystemWatcher watcher = new FileSystemWatcher();

            watcher.Path = project.Path;

            watcher.IncludeSubdirectories = true; //  indicating whether subdirectories within the specified path should be monitored

            watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName |
                NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;

            watcher.Filter = ""; // To watch for changes in all files

            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.Created += new FileSystemEventHandler(OnChanged);
            watcher.Deleted += new FileSystemEventHandler(OnChanged);
            watcher.Renamed += new RenamedEventHandler(OnRenamed);

            watcher.EnableRaisingEvents = true;

        }

        private static void OnChanged(object source, FileSystemEventArgs e)
        {
        }

        private static void OnRenamed(object source, RenamedEventArgs e)
        {
        }
    }
}
